<template src="./template.html"></template>
<style src="./style.css" scoped></style>
<script>
    import defaultImg from 'assets/images/default-img.png';
    import faceNiceIcon from "assets/images/smile_face_0.png";
    import faceNormalIcon from "assets/images/smile_face_1.png";
    import faceBadIcon from "assets/images/smile_face_2.png";
    import starIcon from "assets/images/stars-gray-bg.png";
    import starActiveIcon from "assets/images/red-star.png";

    import api from "api/api";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import route from 'constants/route';
    import constant from 'constants/constant';
    import modal from "utils/modal";

    export default{
        data(){
            return{
                defaultImg: defaultImg,
                order: {},
                faceNiceIcon: faceNiceIcon,
                faceNormalIcon: faceNormalIcon,
                faceBadIcon: faceBadIcon,
                starIcon: starIcon,
                starActiveIcon: starActiveIcon,
                orderDesStars: 5, //默认5星好评
                orderSpeedStars: 5, //默认5星好评
                orderAttitudeStars: 5, //默认5星好评
                rateInfo:[] //所有商品评价信息集
            }
        },
        created() {
            let self = this;
            let config = weex.config;
            let oid = config.oid ? config.oid : null;

            api.sendHttp("getRateOrderApi",{oid: oid}).then(res=>{
                self.order = res.data;
                if(self.order.goods){
                    self.order.goods.map((v,i,arr)=>{
                        if(!v.grade){
                            v.grade = '1';
                        }
                        return v;
                    });
                }
            },
            res=>{
                modal.toast(res.msg || "获取评价订单详情失败");
            });
        },
        methods: {
            subEvaluate(){
                let self = this;
                if(self.checkRate()){
                    let params = {
                        oid: self.order.oid,
                        description_score: self.orderDesStars,
                        speed_score: self.orderSpeedStars,
                        attitude_score: self.orderAttitudeStars,
                        rateinfo: JSON.stringify(self.rateInfo)
                    };
                    api.sendHttp("subRateOrderApi",params).then(res=>{
                        modal.toast(res.msg || "评价成功");
                        notification.send(constant.EVENT_ORDER_RATE,res.data);
                        navigator.pop();
                    },
                    res=>{
                        modal.toast(res.msg || "评价失败");
                    });
                }
            },
            goToGoodsDetail(goods){
                let self = this;
                if(goods && goods.id){
                    let options = {
                        url: route.detail_index,
                        title: "商品详情",
                        params: {
                            id: goods.id
                        }
                    };
                    navigator.push(options);
                }
            },
            checkRate(){
                let flag = true;
                let self = this;
                let _goods = self.order.goods;

                _goods.map((value,key,arr)=>{
                    if(!value.grade){ //未进行任何评价提示错误信息
                        modal.toast("您还有商品未进行任何评价");
                        flag = false;
                        return false;
                    }

                    if(value.remark && value.remark.length && value.remark.length > 200) {
                        modal.toast("商品评价内容不能超过200字");
                        flag = false;
                        return false;
                    }

                    let _arr = [];
                    _arr.push(value.ogid);
                    _arr.push(value.grade);
                    _arr.push(value.remark);

                    self.rateInfo.push(_arr);
                });

                return flag;
            },
            evaluateGoods(index,goods,star){ //给商品点评
                if(goods && star){
                    goods.grade = star;
                    this.order.goods.$set(index,goods);
                }
            },
            evaluateOrderDesStars(star){ //描述相符打分
                if(star) this.orderDesStars = star;
            },
            evaluateOrderSpeedStars(star){ //发货速度打分
                if(star) this.orderSpeedStars = star;
            },
            evaluateOrderAttitudeStars(star){ //服务态度打分
                if(star) this.orderAttitudeStars = star;
            }
        }
    }
</script>
